package org.eredlab.g4.rif.report.word;

import java.awt.Color;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.lowagie.text.Cell;
import com.lowagie.text.Chunk;
import com.lowagie.text.Document;
import com.lowagie.text.DocumentException;
import com.lowagie.text.Element;
import com.lowagie.text.Font;
import com.lowagie.text.PageSize;
import com.lowagie.text.Paragraph;
import com.lowagie.text.Table;
import com.lowagie.text.pdf.BaseFont;
import com.lowagie.text.rtf.RtfWriter2;

import org.eredlab.g4.ccl.cuc.ChineseUpperCaser;
import org.eredlab.g4.ccl.datastructure.Dto;
import org.eredlab.g4.ccl.datastructure.impl.BaseDto;
import org.eredlab.g4.ccl.util.G4Utils;
import org.eredlab.g4.rif.report.excel.ExcelData;
import org.eredlab.g4.rif.report.excel.ExcelFiller;
import org.eredlab.g4.rif.report.excel.ExcelTemplate;
import com.lowagie.text.rtf.field.RtfField;
import com.lowagie.text.rtf.style.RtfFont;
import java.io.ByteArrayOutputStream;

public class WordExporter {
	String[] param;
	List fieldsList;
	public void createDocContext(ByteArrayOutputStream bos,String[] param, List fieldsList)
			throws DocumentException, IOException {		
		String titleString = param[0] + "             " + param[1];
		String contextString0= param[2];
		
		RtfFont titleFont = new RtfFont("楷体_GB2312",14,Font.NORMAL);
		String contextString = "阿克苏地区政府采购中心经过" + param[3] + "采购方式，"
		+ "确定你公司为本次采购货物供货商。" + "请贵公司接此通知后，" + "按招标文件的条款及投标书的承诺、"
		+ "价格及时与" + param[4] + " 签订合同，联系人：" + param[5] + "，联系电话："
		+ param[6] + "；"
		+ "并在规定的时间内交付货物，提供相关的技术服务。合同及验货单一式五份，买、卖双方各留一份，"
		+ "三份留待以后与财政局采购中心、采购办、国库科办理采购资金结算手续。";					
		String contextString1 = "中标供货商联系人：" + param[7] + " ；联系电话： " + param[8]	+ "。";		
		String contextString3 = "请贵公司按照中标总价";		
		String contextString2 = "人民币：（大写）" + new ChineseUpperCaser(param[9]) + "（" + param[9] + "元）提供货物";
		String contextString4 = "阿克苏地区政府采购中心";		
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd"); 
		String contextString5 = format.format(new Date());		
        //设置纸张大小   
        Document document = new Document(PageSize.A4);   
        //建立一个书写器，与document对象关联   
        RtfWriter2.getInstance(document, bos);   
        document.open();   
        //设置中文字体   
        BaseFont bfChinese = BaseFont.createFont("STSongStd-Light","UniGB-UCS2-H",BaseFont.NOT_EMBEDDED);          
        //标题字体风格          
        //正文字体风格   
        RtfFont contextFont = new RtfFont("楷体_GB2312",14,Font.NORMAL);   
        Paragraph title = new Paragraph(titleString);   
        //设置标题格式对齐方式   
        title.setAlignment(Element.ALIGN_CENTER);   
        title.setFont(titleFont);   
        document.add(title);            
        Paragraph context0 = new Paragraph(contextString0);   
        context0.setAlignment(Element.ALIGN_LEFT);   
        context0.setFont(contextFont);   
        //段间距   
        context0.setSpacingBefore(3);   
        //设置第一行空的列数   
        document.add(context0);       
        Paragraph context = new Paragraph(contextString);   
        context.setAlignment(Element.ALIGN_LEFT);   
        context.setFont(contextFont);   
        //段间距   
        context.setSpacingBefore(3);   
        //设置第一行空的列数   
        context.setFirstLineIndent(20);
        document.add(context);         
        Paragraph context1 = new Paragraph(contextString1);   
        context1.setAlignment(Element.ALIGN_LEFT);   
        context1.setFont(contextFont);   
        //段间距   
        context1.setSpacingBefore(3);   
        //设置第一行空的列数   
        document.add(context1);       
        Paragraph context3 = new Paragraph(contextString3);   
        context3.setAlignment(Element.ALIGN_LEFT);   
        context3.setFont(contextFont);   
        //段间距   
        context3.setSpacingBefore(3);   
        //设置第一行空的列数   
        document.add(context3);              
        Paragraph context2 = new Paragraph(contextString2);   
        context2.setAlignment(Element.ALIGN_LEFT);   
        context2.setFont(contextFont);   
        //段间距   
        context2.setSpacingBefore(3);   
        //设置第一行空的列数   
        document.add(context2);       
        //设置Table表格,创建一个六列的表格   
        Table table = new Table(6);   
        int width[] = {20,40,10,10,10,10};//设置每列宽度比例   
        table.setWidths(width);   
        table.setWidth(100);//占页面宽度比例   
        table.setAlignment(Element.ALIGN_CENTER);//居中   
        table.setAutoFillEmptyCells(true);//自动填满   
        table.setBorderWidth(1);//边框宽度           
		// 设置表头
		Cell headerCell1 = new Cell("表格表头");
		Cell headerCell2 = new Cell("规格型号");
		Cell headerCell3 = new Cell("单位");
		Cell headerCell4 = new Cell("数量");
		Cell headerCell5 = new Cell("单价");
		Cell headerCell6 = new Cell("合价");
		headerCell1.setHeader(true);
		headerCell2.setHeader(true);
		headerCell3.setHeader(true);
		headerCell4.setHeader(true);
		headerCell5.setHeader(true);
		headerCell6.setHeader(true);
		headerCell1.setVerticalAlignment(Element.ALIGN_CENTER);
		headerCell1.setHorizontalAlignment(Element.ALIGN_MIDDLE);
		headerCell2.setVerticalAlignment(Element.ALIGN_CENTER);
		headerCell2.setHorizontalAlignment(Element.ALIGN_MIDDLE);
		headerCell3.setVerticalAlignment(Element.ALIGN_CENTER);
		headerCell3.setHorizontalAlignment(Element.ALIGN_MIDDLE);
		headerCell4.setVerticalAlignment(Element.ALIGN_CENTER);
		headerCell4.setHorizontalAlignment(Element.ALIGN_MIDDLE);
		headerCell5.setVerticalAlignment(Element.ALIGN_CENTER);
		headerCell5.setHorizontalAlignment(Element.ALIGN_MIDDLE);
		headerCell6.setVerticalAlignment(Element.ALIGN_CENTER);
		headerCell6.setHorizontalAlignment(Element.ALIGN_MIDDLE);				
		table.addCell(headerCell1);
		table.addCell(headerCell2);
		table.addCell(headerCell3);
		table.addCell(headerCell4);
		table.addCell(headerCell5);
		table.addCell(headerCell6);
		table.endHeaders();		
        Font fontChinese = new Font(bfChinese,13,Font.NORMAL,Color.GREEN);   
        for (int i = 0; i < fieldsList.size(); i++) {
			BaseDto m = (BaseDto) fieldsList.get(i);
			DecimalFormat df = new DecimalFormat("#.00");
			double purdet_facttotal = new Double(m.getAsString("purdet_facttotal"));
			int purdet_factpuchasingquantity = new Integer(m.getAsString(
					"purdet_factpuchasingquantity"));
			double unitprice = 0;
			try {
				unitprice = new Double(df.format(purdet_facttotal/ purdet_factpuchasingquantity));		
			} catch (Exception e) {			
			}				
			Cell cell1 = new Cell(m.getAsString("prd_name"));
			Cell cell2 = new Cell(m.getAsString("prm_name"));
			Cell cell3 = new Cell(m.getAsString("prd_unit"));
			Cell cell4 = new Cell(m.getAsString("purdet_factpuchasingquantity"));
			Cell cell5 = new Cell("" + unitprice);
			Cell cell6 = new Cell(m.getAsString("purdet_facttotal"));				
			cell1.setVerticalAlignment(Element.ALIGN_CENTER);
			cell1.setHorizontalAlignment(Element.ALIGN_MIDDLE);
			cell2.setVerticalAlignment(Element.ALIGN_CENTER);
			cell2.setHorizontalAlignment(Element.ALIGN_MIDDLE);
			cell3.setVerticalAlignment(Element.ALIGN_CENTER);
			cell3.setHorizontalAlignment(Element.ALIGN_MIDDLE);
			cell4.setVerticalAlignment(Element.ALIGN_CENTER);
			cell4.setHorizontalAlignment(Element.ALIGN_MIDDLE);
			cell5.setVerticalAlignment(Element.ALIGN_CENTER);
			cell5.setHorizontalAlignment(Element.ALIGN_MIDDLE);
			cell6.setVerticalAlignment(Element.ALIGN_CENTER);
			cell6.setHorizontalAlignment(Element.ALIGN_MIDDLE);		
			table.addCell(cell1);
			table.addCell(cell2);
			table.addCell(cell3);
			table.addCell(cell4);
			table.addCell(cell5);
			table.addCell(cell6);
		}           
        document.add(table);         
        Paragraph context4 = new Paragraph(contextString4);   
        context4.setAlignment(Element.ALIGN_RIGHT);   
        context4.setFont(contextFont);   
        //段间距   
        context4.setSpacingBefore(4);   
        //设置第一行空的列数   
        //context3.setFirstLineIndent(20); 
        document.add(context4);        
        Paragraph context5 = new Paragraph(contextString5);   
        context5.setAlignment(Element.ALIGN_RIGHT);   
        context5.setFont(contextFont);   
        //段间距   
        context5.setSpacingBefore(5);   
        //设置第一行空的列数   
        //context3.setFirstLineIndent(20); 
        document.add(context5);                
        RtfFont contextFontx = new RtfFont("楷体_GB2312",14,Font.UNDERLINE);   
        Paragraph context6 = new Paragraph("														   ");
        context6.setFont(contextFontx);
        context6.setAlignment(Element.ALIGN_LEFT);   
        context6.setFont(contextFontx);   
        //段间距   
        context6.setSpacingBefore(5);   
        //设置第一行空的列数   
        //context3.setFirstLineIndent(20); 
        document.add(context6);               
        Paragraph context7 = new Paragraph("经办人：					   审核：				  联系电话：2128602");
        context7.setFont(contextFontx);
        context7.setAlignment(Element.ALIGN_LEFT);   
        context7.setFont(contextFontx);   
        //段间距   
        context7.setSpacingBefore(5);   
        //设置第一行空的列数   
        //context3.setFirstLineIndent(20); 
        document.add(context7);        
        document.close();        
	}

	/**
	 * 导出word
	 * 
	 * @param request
	 * @param response
	 * @throws IOException
	 * @throws DocumentException 
	 */
	public void export(HttpServletRequest request, HttpServletResponse response)
			throws IOException, DocumentException {
		response.setContentType("application/vnd.ms-word");
		String filename = G4Utils.encodeChineseDownloadFileName(request,
				"招标通知书.doc");
		response.setHeader("Content-Disposition", "attachment; filename="
				+ filename + ";");				
		ByteArrayOutputStream bos = new ByteArrayOutputStream();
		createDocContext(bos,getParam(),getFieldsList());
		ServletOutputStream os = response.getOutputStream();
		os.write(bos.toByteArray());
		os.flush();
		os.close();				
	}

	public String[] getParam() {
		return param;
	}

	public void setParam(String[] param) {
		this.param = param;
	}

	public List getFieldsList() {
		return fieldsList;
	}

	public void setFieldsList(List fieldsList) {
		this.fieldsList = fieldsList;
	}
}
